﻿using System;
using System.Globalization;
using BazookaBird.Sl.Data.Model;

namespace BazookaBird.Sl.Data.Helpers
{
    public static class TfsConnectionHelper
    {
        public static TFSData CreateTfsDataServiceContext(TfsCredentials tfsCredentials)
        {
            var serviceUri = new Uri(tfsCredentials.CollectionPath);
            var context = new TFSData(serviceUri);

            context.SendingRequest += (s, e) =>
            {
                var credentials = string.Format(@"{0}:{1}", tfsCredentials.UserName, tfsCredentials.Password);
                e.RequestHeaders["Authorization"] = string.Format(CultureInfo.InvariantCulture, "Basic {0}", Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(credentials)));
            };

            return context;
        }
    }
}
